/**
 *
 */
package MongoDAL;

import Acquaintance.IALocation;
import Acquaintance.IAPlace;
import DALFactory.IDataMapper2;
import Entity.ELocation;
import Entity.EPlace;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import java.util.ArrayList;

/**
 * Mongdo数据层部分<br> 负责实现DataMapper2接口的类<br>
 *
 * @author moe
 */
public class MongoDataMapper2 implements IDataMapper2 {

    /**
     *
     * @param name
     * @return
     */
    public ArrayList<IAPlace> SearchByName(String name) {
        ArrayList<IAPlace> result = new ArrayList<IAPlace>();
        MongoStatement mstm = new MongoStatement();
        DBCollection coll = mstm.QueryTable("place");
        DBObject myDoc = new BasicDBObject();
        DBObject target = new BasicDBObject();
        double[] location = {lat, lng};
        target.put("$near", location);
        myDoc.put("name", name);
        myDoc.put("location", target);
        DBCursor cur = coll.find(myDoc);
        while (cur.hasNext()) {
            DBObject doc = cur.next();
            result.add(mapPlace(doc));
        }
        return result;
    }

    /**
     *
     * @param Type
     * @return
     */
    public ArrayList<IAPlace> SearchByType(String Type) {
        ArrayList<IAPlace> result = new ArrayList<IAPlace>();
        MongoStatement mstm = new MongoStatement();
        DBCollection coll = mstm.QueryTable("place");
        DBObject myDoc = new BasicDBObject();
        DBObject target = new BasicDBObject();
        double[] location = {lat, lng};
        target.put("$near", location);
        myDoc.put("Types", Type);
        myDoc.put("location", target);
        DBCursor cur = coll.find(myDoc);

        while (cur.hasNext()) {
            DBObject doc = cur.next();
            result.add(mapPlace(doc));

        }
        return result;
    }
    
    public IAPlace getPlaceByID(String ID)
    {
        MongoStatement mstm = new MongoStatement();
        DBCollection coll = mstm.QueryTable("place");
        DBObject target = new BasicDBObject();
        target.put("_id", ID);
        DBObject myDoc = coll.findOne(target);   
        return mapPlace(myDoc);
    }
    
     public boolean exiPlaceByID(String ID)
    {
        MongoStatement mstm = new MongoStatement();
        DBCollection coll = mstm.QueryTable("place");
        DBObject target = new BasicDBObject();
        target.put("_id", ID);
        DBObject myDoc = coll.findOne(target);   
        if(myDoc!=null)
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }

    /**
     *
     * @param doc
     * @return
     */
    private IAPlace mapPlace(DBObject doc) {
        IAPlace place = null;
        IALocation location = null;
        place = CreatePlace();
        String name = doc.get("name").toString();
        String vicinity = doc.get("vicinity").toString();
        String Type = doc.get("Types").toString();
        String ID = doc.get("_id").toString();
        DBObject locat = (DBObject) doc.get("location");
        String[] Types = Type.split(",");
        location = CreateLocation(Double.parseDouble(locat.get("lat").toString()), Double.parseDouble(locat.get("lng").toString()));
        place.setID(ID);
        place.setLocation((ELocation) location);
        place.setName(name);
        place.setType(Types);
        place.setVicinity(vicinity);
        return place;

    }

    /**
     *
     * @return
     */
    private IAPlace CreatePlace() {
        return new EPlace();
    }

    /**
     *
     * @param lat
     * @param lng
     * @return
     */
    private IALocation CreateLocation(double lat, double lng) {
        return new ELocation(lat, lng);
    }
}
